//
//  JNTMoreViewController.m
//  CRSCMOA
//
//  Created by ANGELINE on 16/8/11.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "JNTMoreViewController.h"
#import "PersonalNeedCollectionViewCell.h"
#import "homeModel.h"

@interface JNTMoreViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    NSIndexPath *_indexPath;
}
@property (nonatomic, strong) UICollectionView *collectionView;   //分区一布局

@end

@implementation JNTMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setUpNavgationBar];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置导航条
- (void)setUpNavgationBar
{
    self.title = @"增加功能";
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"＜" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick)];    [self.view addSubview:self.collectionView];
    
    //添加长按手势
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGR.minimumPressDuration = 1.0;
    [self.collectionView addGestureRecognizer:longPressGR];
    
    
}

#pragma mark  事件回调
//left按钮回调
- (void)leftBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

//长按手势回调
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        
        CGPoint point = [gesture locationInView:self.collectionView];
        _indexPath = [self.collectionView indexPathForItemAtPoint:point];
        if (self.block) {
            self.block([self.showKeyMArray[_indexPath.item] intValue]); // 把需要给上个界面添加的值传过去
        }
        [self.showKeyMArray removeObjectAtIndex:_indexPath.item];   // 删除本界面长按的值
        
        [self.collectionView reloadData];
        // 还得在 头添加个 完成
    }
}

#pragma Mark九宫格布局
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 127, kScreenWidth, kScreenHeight-64-125-49) collectionViewLayout:flowLayout];
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}


#pragma Mark - -------UICollectionView的代理方法------

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.showKeyMArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PersonalNeedCollectionViewCell *CELL = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
    CELL.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    CELL.pointView.alpha = 0;
    
    
    CELL.cellLabel.text = [self getShowTitleArray][indexPath.item];
//    CELL.cellImageView.image = [UIImage imageNamed:[self getShowImgArray][indexPath.item]];
    NSNumber *index = self.showKeyMArray[indexPath.item];
    homeModel *model = self.dataSourceMArr[[index intValue]];
    CELL.cellImageView.image = [UIImage imageNamed:[self getImgNamesWithID:[NSString stringWithFormat:@"%@", model.ID]]];
    
    
    return CELL;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(0, 0, 1, 1);
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return CGSizeMake((kScreenWidth-2)/4, kScreenWidth/4);
    }
    else if (indexPath.section == 1){
        
        return CGSizeMake(80, 80);
        
    }
    return CGSizeMake(0, 0);
}


#pragma mark   item点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
 
        //        获取当前点击item的下标
        NSNumber *index = self.showKeyMArray[indexPath.item];
        //        从model中取出相对应的值
        homeModel *model = self.dataSourceMArr[[index intValue]];
        //        跳转到相对应的VC中
        UIViewController *vc = [NSClassFromString([self getClassNamesWithID:[NSString stringWithFormat:@"%@", model.ID]]) new];
        vc.title = model.name;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:NO];
    }
    

#pragma mark  ---对数据进行封装--
// 这里根据 返回 数据里面的ID 来判断显示哪个VC，根据判断需求修改(这里到时候把要跳转的VC名称 对应写好)
- (NSString *)getClassNamesWithID:(NSString *)classID {
    NSDictionary *classNames = @{@"33":@"OneCartoonViewController",
                                 @"34":@"OfficialDocumentViewController",
                                 @"35":@"ApplyViewController",
                                 @"36":@"ApplyViewController",
                                 @"37":@"ApplyViewController",
                                 @"38":@"ApplyViewController",
                                 @"39":@"CalendarViewController",
                                 @"40":@"ApplyViewController",
                                 @"41":@"ApplyViewController",
                                 @"42":@"ApplyViewController",
                                 @"47":@"ApplyViewController",
                                 @"48":@"EntranceGuardViewController",
                                 @"81":@"ApplyViewController",
                                 @"88":@"ApplyViewController",
                                 @"89":@"ApplyViewController",
                                 @"90":@"OneCartoonViewController"};
    
    
    return [classNames objectForKey:classID];
}

// 获取需要显示的title
- (NSArray *)getShowTitleArray {
    
    NSMutableArray *titleArray = [NSMutableArray array];
    for (int i = 0; i < self.showKeyMArray.count; i++) {
        NSNumber *index = self.showKeyMArray[i];
        homeModel *model = self.dataSourceMArr[[index intValue]];
        [titleArray addObject:model.name];
    }
    return titleArray;
}


// 根据 数据里面的ID 获取本地图片名称
- (NSString *)getImgNamesWithID:(NSString *)classID {
    NSDictionary *imgNames = @{@"33":@"home_btn_approve_f",
                               @"34":@"home_btn_workbench_f",
                               @"35":@"home_btn_news_f",
                               @"36":@"home_btn_signin_f",
                               @"37":@"home_btn_visitor_f",
                               @"38":@"home_btn_package_f",
                               @"39":@"home_btn_schedule_f",
                               @"40":@"home_btn_report_f",
                               @"41":@"home_btn_parking_f",
                               @"42":@"home_btn_personal_f",
                               @"47":@"ic_cancel",  // 这个图片没找到
                               @"48":@"ic_cancel",  // 这个图片没找到
                               @"81":@"home_btn_news_approval_f",
                               @"88":@"ic_meetingroom",
                               @"89":@"ic_meetingroom_check",
                               @"90":@"home_btn_video_f"};
    
    
    return [imgNames objectForKey:classID];
}


@end
